package ggze.GGZEServer.home.service.impl;

import ggze.GGZEServer.home.dao.inter.ReRelationDao;
import ggze.GGZEServer.home.dao.inter.SoBlanceDao;
import ggze.GGZEServer.home.dao.inter.SoIncomeRecordDao;
import ggze.GGZEServer.home.dao.inter.SoTeamUserDao;
import ggze.GGZEServer.home.entity.ReRelation;
import ggze.GGZEServer.home.entity.SoBlance;
import ggze.GGZEServer.home.entity.SoIncomeRecord;
import ggze.GGZEServer.home.entity.SoIndent;
import ggze.GGZEServer.home.service.BlanceService;
import ggze.GGZEServer.home.service.ExpendService;
import ggze.GGZEServer.home.service.IncomeService;
import ggze.actuator.ActuatorController;
import ggze.esp.dao.inter.FspUserDao;
import ggze.esp.entity.FspUser;
import ggze.tool.ReturnBody;
import ggze.tool.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Time;

@Service
public class IncomeServiceImpl implements IncomeService {
    @Autowired
    private SoBlanceDao soBlanceDao;
    @Autowired
    private SoIncomeRecordDao soIncomeRecordDao;
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private FspUserDao fspUserDao;
    @Autowired
    private ReRelationDao reRelationDao;
    @Autowired
    private SoTeamUserDao soTeamUserDao;
    @Autowired
    private BlanceService blanceService;


    //寄售的订单产生应收
    @Override
    @Transactional
    public ReturnBody createIncome(SoIndent soIndent) {
        ReturnBody returnBody  =  new ReturnBody();
        //全部的应收有5条，持有人的本金利息和赠送，第一分享人，和第二分享人
        FspUser user = fspUserDao.isSelect("uuid",soIndent.getF_BY_USER_ID());
        if(!blanceService.createBlance(user)){
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnBody;
        }
        //持有人
        if(!this.authorLS(soIndent,user)){
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnBody;
        }
        //第一分享人
        user = fspUserDao.isSelect("uuid",soIndent.getF_FX_FIRST());
        if(user!=null){
            if(!this.firstLS(soIndent,user)){
                returnBody.setCode("1");
                returnBody.setTitle("系统错误");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return returnBody;
            }
        }
        //第二分享人
        user = fspUserDao.isSelect("uuid",soIndent.getF_FX_SECOND());
        if(user!=null){
            if(!this.firstLS(soIndent,user)){
                returnBody.setCode("1");
                returnBody.setTitle("系统错误");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return returnBody;
            }
        }
        returnBody.setCode("0");
        returnBody.setTitle("应收执行成功！");
        return returnBody;
    }

    @Override
    public ReturnBody endIncome(SoIndent soIndent) {
        return null;
    }

    //持有人流水新增。应收账户变动
    private boolean authorLS(SoIndent soIndent,FspUser user){
        try {
            //流水新增
            String time = TimeUtils.getTimeFormat();
            SoIncomeRecord soIncomeRecord = new SoIncomeRecord();
            soIncomeRecord.setUuid("");
            soIncomeRecord.setF_USRE_ID(user.getUuid());
            soIncomeRecord.setF_USER_NAME(user.getF_NICKNAME());
            soIncomeRecord.setF_METHOD_TIME(time);
            soIncomeRecord.setF_CREATE_TIME(time);

            Double ze = 0.00;
            ze=ze+soIndent.getF_MONEY();
            soIncomeRecord.setF_MONEY(soIndent.getF_MONEY());
            soIncomeRecord.setF_SELES1("BJ"+soIndent.getUuid());
            soIncomeRecord.setF_NOTE("寄售本金收入");
            actuatorController.execute(soIncomeRecord,"add",soIncomeRecordDao);

            ReRelation reRelation = reRelationDao.byMax(soIndent.getF_MONEY());
            ze=ze+(soIndent.getF_MONEY()/reRelation.getF_XFJE())*reRelation.getF_ZSJE();
            soIncomeRecord.setF_MONEY((soIndent.getF_MONEY()/reRelation.getF_XFJE())*reRelation.getF_ZSJE());
            soIncomeRecord.setF_SELES1("LX"+soIndent.getUuid());
            soIncomeRecord.setF_NOTE("寄售利息收入");
            actuatorController.execute(soIncomeRecord,"add",soIncomeRecordDao);

            ze=ze+(soIndent.getF_MONEY()/reRelation.getF_XFJE())*reRelation.getF_JLJE();
            soIncomeRecord.setF_MONEY((soIndent.getF_MONEY()/reRelation.getF_XFJE())*reRelation.getF_JLJE());
            soIncomeRecord.setF_SELES1("ZS"+soIndent.getUuid());
            soIncomeRecord.setF_NOTE("寄售赠送收入");
            actuatorController.execute(soIncomeRecord,"add",soIncomeRecordDao);

            SoBlance soBlance = soBlanceDao.isSelect("f_user_id",user.getUuid());
            soBlance.setF_INCOME_JE(ze+soBlance.getF_INCOME_JE());
            soBlance.setF_METHOD_TIME(time);
            actuatorController.execute(soBlance,"update",soBlanceDao);
            return true;
        }catch (Exception e){
            return false;
        }
    }
    //第一分享人流水---
    private boolean firstLS(SoIndent soIndent,FspUser user){
        try {
            String time = TimeUtils.getTimeFormat();
            SoIncomeRecord soIncomeRecord = new SoIncomeRecord();
            soIncomeRecord.setUuid("");
            soIncomeRecord.setF_USRE_ID(user.getUuid());
            soIncomeRecord.setF_USER_NAME(user.getF_NICKNAME());
            soIncomeRecord.setF_METHOD_TIME(time);
            soIncomeRecord.setF_CREATE_TIME(time);

            ReRelation reRelation = reRelationDao.byMax(soIndent.getF_MONEY());

            Double ze = 0.00;
            ze=ze+soIndent.getF_MONEY()*reRelation.getF_FIRST_MULTIPLE();
            soIncomeRecord.setF_MONEY(soIndent.getF_MONEY()*reRelation.getF_FIRST_MULTIPLE());
            soIncomeRecord.setF_SELES1("FFX"+soIndent.getUuid());
            soIncomeRecord.setF_NOTE("寄售第一分享人应收");
            actuatorController.execute(soIncomeRecord,"add",soIncomeRecordDao);


            SoBlance soBlance = soBlanceDao.isSelect("f_user_id",user.getUuid());
            soBlance.setF_INCOME_JE(ze+soBlance.getF_INCOME_JE());
            soBlance.setF_METHOD_TIME(time);
            actuatorController.execute(soBlance,"update",soBlanceDao);
            return true;
        }catch (Exception e){
            return false;
        }
    }
    //第二分享人流水---
    private boolean secondLS(SoIndent soIndent,FspUser user){
        try {
            String time = TimeUtils.getTimeFormat();
            SoIncomeRecord soIncomeRecord = new SoIncomeRecord();
            soIncomeRecord.setUuid("");
            soIncomeRecord.setF_USRE_ID(user.getUuid());
            soIncomeRecord.setF_USER_NAME(user.getF_NICKNAME());
            soIncomeRecord.setF_METHOD_TIME(time);
            soIncomeRecord.setF_CREATE_TIME(time);

            ReRelation reRelation = reRelationDao.byMax(soIndent.getF_MONEY());

            Double ze = 0.00;
            ze=ze+soIndent.getF_MONEY()*reRelation.getF_FIRST_MULTIPLE();
            soIncomeRecord.setF_MONEY(soIndent.getF_MONEY()*reRelation.getF_SECOND_MULTIPLE());
            soIncomeRecord.setF_SELES1("SFX"+soIndent.getUuid());
            soIncomeRecord.setF_NOTE("寄售第二分享人应收");
            actuatorController.execute(soIncomeRecord,"add",soIncomeRecordDao);


            SoBlance soBlance = soBlanceDao.isSelect("f_user_id",user.getUuid());
            soBlance.setF_INCOME_JE(ze+soBlance.getF_INCOME_JE());
            soBlance.setF_METHOD_TIME(time);
            actuatorController.execute(soBlance,"update",soBlanceDao);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
